define(function (require, exports, module) {

    var $ = require("zepto");

    function lottery(t, e, n, i, a, o) {
        this.conNode = t;
        this.background = null;
        this.backCtx = null;
        this.mask = null;
        this.maskCtx = null;
        this.lottery = null;
        this.lotteryType = "image";
        this.cover = e || "#000";
        this.coverType = n;
        this.pixlesData = null;
        this.width = i;
        this.height = a;
        this.lastPosition = null;
        this.drawPercentCallback = o;
        this.vail = false;
    }

    lottery.prototype = {
        createElement: function (elementName, attr) {
            var node = document.createElement(elementName);
            for (var i in attr)
                node.setAttribute(i, attr[i]);
            return node;
        },
        getTransparentPercent: function (t, e, n) {
            for (var i = t.getImageData(0, 0, e, n), a = i.data, o = [], r = 0, s = a.length; s > r; r += 4) {
                var l = a[r + 3];
                128 > l && o.push(r);
            }
            return(100 * (o.length / (a.length / 4))).toFixed(2);
        },
        resizeCanvas: function (t, e, n) {
            t.width = e, t.height = n, t.getContext("2d").clearRect(0, 0, e, n)
        },
        resizeCanvas_w: function (t, e, n) {
            t.width = e, t.height = n, t.getContext("2d").clearRect(0, 0, e, n), this.vail ? this.drawLottery() : this.drawMask()
        },
        drawPoint: function (t, e) {
            this.maskCtx.beginPath();
            this.maskCtx.arc(t, e, 30, 0, 2 * Math.PI);
            this.maskCtx.fill();
            this.maskCtx.beginPath();
            this.maskCtx.lineWidth = 60;
            this.maskCtx.lineCap = this.maskCtx.lineJoin = "round";
            this.lastPosition && this.maskCtx.moveTo(this.lastPosition[0], this.lastPosition[1]);
            this.maskCtx.lineTo(t, e);
            this.maskCtx.stroke();
            this.lastPosition = [t, e];
            (this.mask.style.zIndex = 20) === this.mask.style.zIndex ? 21 : 20;
        },
        bindEvent: function () {
            var t = this, e = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase()), n = e ? "touchstart" : "mousedown", i = e ? "touchmove" : "mousemove";
            if (e)
                t.conNode.addEventListener("touchmove", function (t) {
                    a && t.preventDefault(), t.cancelable ? t.preventDefault() : window.event.returnValue = !1
                }, !1), t.conNode.addEventListener("touchend", function () {
                    a = !1;
                    var e = t.getTransparentPercent(t.maskCtx, t.width, t.height);
                    e >= 50 && "function" == typeof t.drawPercentCallback && t.drawPercentCallback()
                }, !1);
            else {
                var a = !1;
                t.conNode.addEventListener("mouseup", function (e) {
                    e.preventDefault(), a = !1;
                    var n = t.getTransparentPercent(t.maskCtx, t.width, t.height);
                    n >= 50 && "function" == typeof t.drawPercentCallback && t.drawPercentCallback()
                }, !1)
            }
            this.mask.addEventListener(n, function (n) {
                n.preventDefault(), a = !0;
                var i = e ? n.touches[0].pageX : n.pageX || n.x, o = e ? n.touches[0].pageY : n.pageY || n.y;
                t.drawPoint(i, o, a)
            }, !1), this.mask.addEventListener(i, function (n) {
                if (n.preventDefault(), !a)
                    return!1;
                n.preventDefault();
                var i = e ? n.touches[0].pageX : n.pageX || n.x, o = e ? n.touches[0].pageY : n.pageY || n.y;
                t.drawPoint(i, o, a)
            }, !1);
        },
        drawLottery: function () {
            if ("image" == this.lotteryType) {
                var t = new Image, e = this;
                t.onload = function () {
                    this.width = e.width, this.height = e.height, e.resizeCanvas(e.background, e.width, e.height), e.backCtx.drawImage(this, 0, 0, e.width, e.height), e.drawMask()
                }, t.src = this.lottery
            } else if ("text" == this.lotteryType) {
                this.width = this.width, this.height = this.height, this.resizeCanvas(this.background, this.width, this.height), this.backCtx.save(), this.backCtx.fillStyle = "#FFF", this.backCtx.fillRect(0, 0, this.width, this.height), this.backCtx.restore(), this.backCtx.save();
                var n = 30;
                this.backCtx.font = "Bold " + n + "px Arial", this.backCtx.textAlign = "center", this.backCtx.fillStyle = "#F60", this.backCtx.fillText(this.lottery, this.width / 2, this.height / 2 + n / 2), this.backCtx.restore(), this.drawMask()
            }
        },
        drawMask: function () {
            if ("color" === this.coverType) {
                this.maskCtx.fillStyle = this.cover;
                this.maskCtx.fillRect(0, 0, this.width, this.height);
                this.maskCtx.globalCompositeOperation = "destination-out";
            } else if ("image" === this.coverType) {
                var image = new Image;
                var that = this;
                image.onload = function () {
                    that.resizeCanvas(that.mask, that.width, that.height);
                    /android/i.test(navigator.userAgent.toLowerCase());
                    that.maskCtx.globalAlpha = .98;
                    that.maskCtx.drawImage(this, 0, 0, this.width, this.height, 0, 0, that.width, that.height);
                    var t = 50;
                    var n = $("#ca-tips").val();
                    var i = that.maskCtx.createLinearGradient(0, 0, that.width, 0);
                    i.addColorStop("0", "#fff");
                    i.addColorStop("1.0", "#000");
                    that.maskCtx.font = "Bold " + t + "px Arial";
                    that.maskCtx.textAlign = "left";
                    that.maskCtx.fillStyle = i;
                    that.maskCtx.fillText(n, that.width / 2 - that.maskCtx.measureText(n).width / 2, 100);
                    that.maskCtx.globalAlpha = 1;
                    that.maskCtx.globalCompositeOperation = "destination-out";
                };
                image.src = this.cover;
            }
        },
        init: function (t, e) {
            if (t) {
                this.lottery = t;
                this.lottery.width = this.width;
                this.lottery.height = this.height;
                this.lotteryType = e || "image";
                this.vail = true;
            }
            if (this.vail) {
                this.background = this.background || this.createElement("canvas", {style: "position:fixed;left:50%;top:0;width:640px;margin-left:-320px;height:100%;background-color:transparent;"});
            }
            this.mask = this.mask || this.createElement("canvas", {style: "position:fixed;left:50%;top:0;width:640px;margin-left:-320px;height:100%;background-color:transparent;"});
            this.mask.style.zIndex = 20;
            this.conNode.innerHTML.replace(/[\w\W]| /g, "") || (this.vail && this.conNode.appendChild(this.background), this.conNode.appendChild(this.mask), this.bindEvent());
            this.vail && (this.backCtx = this.backCtx || this.background.getContext("2d"));
            this.maskCtx = this.maskCtx || this.mask.getContext("2d");
            this.vail ? this.drawLottery() : this.drawMask();
            var $node = this;
            window.addEventListener("resize", function () {
                $node.width = document.documentElement.clientWidth;
                $node.height = document.documentElement.clientHeight;
                $node.resizeCanvas_w($node.mask, $node.width, $node.height);
            }, false);
        }
    };
    module.exports = lottery;
});